
package com.foxmail.aroundme.boring.utils.helper;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.foxmail.aroundme.boring.BuildConfig;
import com.socks.library.KLog;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.TimeZone;

public class BaseInfoHelper {

    public static String getWifiMac(Context context) {
        try {
            String ret = null;

            WifiManager wifiMgr = (WifiManager) context
                    .getSystemService(android.content.Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
            if (wifiInfo != null) {
                ret = wifiInfo.getMacAddress();
            }

            return ret;
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                KLog.e("Failed to get the wifiMac info.", e);
            }
            return "";
        }
    }

    public static String getMmcID() {
        // read '/sys/block/mmcblk%d/device/cid'
        // available before SD card mounted, available on user build devices.
        // This operation would complete in fixed time
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < 4; i++) {
            String command = String.format("/system/bin/cat /sys/block/mmcblk%d/device/cid", i);

            try {
                java.lang.Process p = Runtime.getRuntime().exec(command);

                InputStream inStream = p.getInputStream();
                InputStreamReader inReader = new InputStreamReader(inStream);
                BufferedReader inBuffer = new BufferedReader(inReader);

                String s;
                while ((s = inBuffer.readLine()) != null && s.length() > 0) {
                    result.append(s).append(" ");
                }
            } catch (Exception e) {
            }
        }

        return result.toString();
    }

    public static String getFreeMemoryKBs() {
        // read /proc/meminfo to find memfree 'MemFree: 143632 kB'
        // This operation would complete in fixed time
        String command = "/system/bin/cat /proc/meminfo";

        try {
            java.lang.Process p = Runtime.getRuntime().exec(command);

            InputStream inStream = p.getInputStream();
            InputStreamReader inReader = new InputStreamReader(inStream);
            BufferedReader inBuffer = new BufferedReader(inReader);

            String s;
            while ((s = inBuffer.readLine()) != null && s.length() > 0) {
                if (s.startsWith("MemFree:")) {
                    return s;
                }
            }
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                KLog.e("Failed to get the Mem info.", e);
            }
        }
        return "unknown";
    }

    public static String getSN() {
        try {
            String ret = "";
            ret = getSystemProperty("ro.serialno");
            if (!TextUtils.isEmpty(ret)) {
                return ret;
            }

            ret = getSystemProperty("ro.hw.dxos.SN");
            return ret;
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                KLog.e("Failed to get the sn info.", e);
            }
            return "";
        }
    }

    public static String getPkgVersionName(Context context) {
        try {
            String pkgName = context.getPackageName();
            PackageInfo manager = context.getPackageManager().getPackageInfo(pkgName, 0);
            if (manager != null) {
                return manager.versionName;
            }
        } catch (Exception e) {
            if (BuildConfig.DEBUG)
                KLog.e("Failed to get PkgVersionName!", e);
        }
        return "";
    }

    public static int getPkgVersionCode(Context context) {
        try {
            String pkgName = context.getPackageName();
            PackageInfo manager = context.getPackageManager().getPackageInfo(pkgName, 0);
            if (manager != null) {
                return manager.versionCode;
            }
        } catch (Exception e) {
            if (BuildConfig.DEBUG)
                KLog.e("Failed to get PkgVersionCode!", e);
        }
        return -1;
    }

    public static String getManufacturer() {
        try {
            return Build.MANUFACTURER;
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                KLog.e("Failed to get the hw info.", e);
            }

            return "";
        }
    }


    public static String getIMEI(Context context) {
        try {
            TelephonyManager teleMgr;
            teleMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return teleMgr.getDeviceId();
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                KLog.e("Failed to get the hw info.", e);
            }
            return "";
        }
    }

    public static String getIMSI(Context context) {
        try {
            TelephonyManager teleMgr;
            teleMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return teleMgr.getSubscriberId();
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                KLog.e("Failed to get the IMSI info!", e);
            }
        }
        return "";
    }


    public static String getOperatorName(Context context) {
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return telephonyManager.getNetworkOperatorName();
        } catch (Exception e) {
            return "";
        }
    }

    public static String getAndroidVersion() {
        try {
            return String.valueOf(Build.VERSION.SDK_INT);
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                KLog.e("Failed to get the androidVersion info.", e);
            }
            return "";
        }
    }

    public static String getNetworkType(Context context) {
        try {
            context = context.getApplicationContext();
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
            if (netInfo != null) {
                String type = netInfo.getTypeName();
                if ("mobile".equals(type.toLowerCase())) {
                    type = netInfo.getSubtypeName();
                }
                return type;
            }
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                KLog.e("Failed to get the netWorkType info.", e);
            }
        }
        return "none";
    }

    public static String getSystemProperty(String name) {
        Class<?> c;
        try {
            c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            return (String) get.invoke(c, name);
        } catch (ClassNotFoundException e) {
            if (BuildConfig.DEBUG) {
                KLog.w("getSystemProperty has ClassNotFoundException", e);
            }
        } catch (SecurityException e) {
            if (BuildConfig.DEBUG) {
                KLog.w("getSystemProperty has SecurityException", e);
            }
        } catch (NoSuchMethodException e) {
            if (BuildConfig.DEBUG) {
                KLog.w("getSystemProperty has NoSuchMethodException", e);
            }
        } catch (IllegalArgumentException e) {
            if (BuildConfig.DEBUG) {
                KLog.w("getSystemProperty has IllegalArgumentException", e);
            }
        } catch (IllegalAccessException e) {
            if (BuildConfig.DEBUG) {
                KLog.w("getSystemProperty has IllegalAccessException", e);
            }
        } catch (InvocationTargetException e) {
            if (BuildConfig.DEBUG) {
                KLog.w("getSystemProperty has InvocationTargetException", e);
            }
        }
        return "";
    }

    public static String getTimeZon() {
        TimeZone tz = TimeZone.getDefault();
        return tz.getDisplayName();
    }


    public static DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displaymetrics);
        return displaymetrics;
    }


}
